﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using Husb.Data;
using Husb.DataUtil;

using CentralOffice.Archive.Data;

using ArchiveTable = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesDataTable;
using ArchiveRow = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesRow;

namespace CentralOffice.Archive.DataAccess
{
    public class ArchiveAdapter : DataAccessAdapter<ArchiveDataSet, ArchiveTable, ArchiveRow>
    {

        private const string SQL_UpdateAuthorityById = "UPDATE Archive.Archive SET Description = @Description WHERE Id = @Id ";

        public readonly string GetMaxOrderNumberStoredProc = "Archive.Archive_MaxOrderNumber";

        public ArchiveAdapter()
        {
            this.TableName = "Archives";

            GetAllStoredProc = "Archive.Archive_SelectArchivesAll";
            InsertStoredProc = "Archive.Archive_InsertArchive";
            UpdateStoredProc = "Archive.Archive_UpdateArchive";
            DeleteStoredProc = "Archive.Archive_DeleteArchive";

            GetByIdStoredProc = "Archive.Archive_SelectArchive";
            GetRowCountStoredProc = "Archive.Archive_SelectArchivesRowCount";
            GetPagedStoredProc = "Archive.Archive_SelectArchivesPaged";
        }
        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.AnsiString, "Name", DataRowVersion.Current);
            db.AddInParameter(cmd, "QueryNumber", DbType.AnsiString, "QueryNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "OrderNumber", DbType.Int32, "OrderNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "SecretLevel", DbType.AnsiString, "SecretLevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "Province", DbType.AnsiString, "Province", DataRowVersion.Current);
            db.AddInParameter(cmd, "City", DbType.AnsiString, "City", DataRowVersion.Current);
            db.AddInParameter(cmd, "County", DbType.AnsiString, "County", DataRowVersion.Current);
            db.AddInParameter(cmd, "ContractCode", DbType.AnsiString, "ContractCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "FiledDate", DbType.DateTime, "FiledDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "CustomerId", DbType.Guid, "CustomerId", DataRowVersion.Current);
            db.AddInParameter(cmd, "CategoryId", DbType.Guid, "CategoryId", DataRowVersion.Current);
            db.AddInParameter(cmd, "Scale", DbType.Int32, "Scale", DataRowVersion.Current);
            db.AddInParameter(cmd, "ConstructForm", DbType.AnsiString, "ConstructForm", DataRowVersion.Current);
            db.AddInParameter(cmd, "DesignStage", DbType.AnsiString, "DesignStage", DataRowVersion.Current);
            db.AddInParameter(cmd, "BelongedTo", DbType.AnsiString, "BelongedTo", DataRowVersion.Current);
            db.AddInParameter(cmd, "ProjectManager", DbType.AnsiString, "ProjectManager", DataRowVersion.Current);
            db.AddInParameter(cmd, "BuildingBlueprint", DbType.String, "BuildingBlueprint", DataRowVersion.Current);
            db.AddInParameter(cmd, "ElectricalBlueprint", DbType.String, "ElectricalBlueprint", DataRowVersion.Current);
            db.AddInParameter(cmd, "MechanicalMaterialManifest", DbType.String, "MechanicalMaterialManifest", DataRowVersion.Current);
            db.AddInParameter(cmd, "ElectricalMaterialManifest", DbType.String, "ElectricalMaterialManifest", DataRowVersion.Current);
            db.AddInParameter(cmd, "SystemSolution", DbType.String, "SystemSolution", DataRowVersion.Current);
            db.AddInParameter(cmd, "ElectricalSolution", DbType.String, "ElectricalSolution", DataRowVersion.Current);

            db.AddInParameter(cmd, "DesignSketch", DbType.String, "DesignSketch", DataRowVersion.Current);
            db.AddInParameter(cmd, "TenderDocumentation", DbType.String, "TenderDocumentation", DataRowVersion.Current);
            db.AddInParameter(cmd, "PreliminaryDesign", DbType.String, "PreliminaryDesign", DataRowVersion.Current);
            db.AddInParameter(cmd, "OtherDocumentation", DbType.String, "OtherDocumentation", DataRowVersion.Current);

            db.AddInParameter(cmd, "KeyWords", DbType.String, "KeyWords", DataRowVersion.Current);
            db.AddInParameter(cmd, "DesignedYear", DbType.String, "DesignedYear", DataRowVersion.Current);

            db.AddInParameter(cmd, "Customer", DbType.AnsiString, "Customer", DataRowVersion.Current);
            //db.AddInParameter(cmd, "QueryNumber", DbType.AnsiString, "QueryNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "Address", DbType.AnsiString, "Address", DataRowVersion.Current);
            db.AddInParameter(cmd, "Telephone1", DbType.AnsiString, "Telephone1", DataRowVersion.Current);
            db.AddInParameter(cmd, "Telephone2", DbType.Binary, "Telephone2", DataRowVersion.Current);
            db.AddInParameter(cmd, "Fax", DbType.AnsiString, "Fax", DataRowVersion.Current);
            db.AddInParameter(cmd, "Email", DbType.AnsiString, "Email", DataRowVersion.Current);
            db.AddInParameter(cmd, "Contact", DbType.AnsiString, "Contact", DataRowVersion.Current);
            db.AddInParameter(cmd, "ContactTitle", DbType.AnsiString, "ContactTitle", DataRowVersion.Current);
            db.AddInParameter(cmd, "ContactPhone", DbType.AnsiString, "ContactPhone", DataRowVersion.Current);

            db.AddInParameter(cmd, "Executor", DbType.AnsiString, "Executor", DataRowVersion.Current);

            db.AddInParameter(cmd, "IsDeleted", DbType.Boolean, "IsDeleted", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsActive", DbType.Boolean, "IsActive", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, "CreatedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedBy", DbType.Guid, "CreatedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ModifiedTime", DbType.DateTime, "ModifiedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "LastModifiedBy", DbType.Guid, "LastModifiedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "Version", DbType.Int32, "Version", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.String, "Description", DataRowVersion.Current);
        }

        public int GetMaxOrderNumber()
        {
            object o = this.GetValue(this.GetMaxOrderNumberStoredProc);

            if (o == DBNull.Value) return 0;

            if (o != null)
                return (int)o;
            else
                return 0;
        }

        public int UpdateAuthority(Guid id, string xml)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("Id", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("Description", DbType.String, xml));
            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(SQL_UpdateAuthorityById, parameters, false, false, null);

            return cnt;
        }

    }
}
